//
//  JXHistoryItemTableCell.m
//

#import "JXHistoryItemTableCell.h"

#define MallTableXSpace 30
#define MallTableYSpace 10
#define MallTableImageSide 100

@implementation JXHistoryItemTableCell

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier
              model:(JXItemModel *)model
           delegate:(id<JXHistoryItemTableCellDelegate>)delegate {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.item = model;
        if (delegate) {
            self.delegate = delegate;
        }
    }
    return self;
}

// getter methods

- (UIImageView *)itemImageView {
    if (!_itemImageView) {
        _itemImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_itemImageView];
    }
    return _itemImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:16.f];
        _nameLabel.numberOfLines = 0;
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)infoLabel {
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.font = [UIFont systemFontOfSize:16.f];
        _infoLabel.numberOfLines = 0;
        _infoLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_infoLabel];
    }
    return _infoLabel;
}

- (UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendBtn.layer.masksToBounds = YES;
        _sendBtn.layer.cornerRadius = 3.f;
        _sendBtn.backgroundColor = JXColorWithRGB(245, 51, 83);
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sendBtn setTitle:@"发送链接" forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_sendBtn];
    }
    return _sendBtn;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.itemImageView.frame =
            CGRectMake(MallTableYSpace, MallTableYSpace, MallTableImageSide, MallTableImageSide + 20);
    self.itemImageView.image = self.item.image;
    CGSize nameSize = CGSizeMake(100, 60);    //[self.item.name calculateSize:CGSizeMake(200, 0)
                                               //font:[UIFont systemFontOfSize:16.f]];
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.itemImageView.frame) + MallTableXSpace,
                                      0, nameSize.width, nameSize.height);
    self.nameLabel.text = self.item.name;

    // info
    CGSize infoSize = CGSizeMake(100, 50);    //[self.item.info calculateSize:CGSizeMake(200, 0)
                                               //font:[UIFont systemFontOfSize:16.f]];
    self.infoLabel.frame = CGRectMake(CGRectGetMaxX(self.itemImageView.frame) + MallTableXSpace,
                                      CGRectGetMaxY(self.nameLabel.frame),
                                      infoSize.width, infoSize.height);
    self.infoLabel.textColor = JXColorWithRGB(245, 53, 81);
    self.infoLabel.text = self.item.info;

    self.sendBtn.frame = CGRectMake((self.bounds.size.width - 100 - MallTableYSpace),
                                    (self.bounds.size.height - 36 - MallTableYSpace), 100, 36);
}

- (void)sendBtnClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendItemInfo:)]) {
        [self.delegate sendItemInfo:self.item];
    }
}

@end
